#include <iostream>
#include <vector>

using namespace std;

class Solution 
{
public:
    vector<vector<int>> generateMatrix(int n) 
    {
        int max = n * n;
        int num = 1;
        int top = 0, left = 0;
        int right = n - 1, bottom = n - 1;
        vector<vector<int>> matrix(n, vector<int>(n, 0));
        while(num <= max)
        {
            for(int i = left; i <= right; i++)
            {
                matrix[top][i] = num;
                num++;
            }
            top++;

            for(int i = top; i <= bottom; i++)
            {
                matrix[i][right] = num;
                num++;
            }
            right--;

            for(int i = right; i >= left; i--)
            {
                matrix[bottom][i] = num;
                num++;
            }
            bottom--;

            for(int i = bottom; i >= top; i--)
            {
                matrix[i][left] = num;
                num++;
            }
            left++;
        }
        return matrix;
    }
};

int main()
{
    Solution s;
    
    // 测试案例1：3x3矩阵
    int n1 = 3;
    cout << "生成 " << n1 << "x" << n1 << " 螺旋矩阵:" << endl;
    vector<vector<int>> result1 = s.generateMatrix(n1);
    for(int i = 0; i < n1; i++)
    {
        for(int j = 0; j < n1; j++)
        {
            cout << result1[i][j] << "\t";
        }
        cout << endl;
    }
    cout << endl;
    
    // 测试案例2：4x4矩阵
    int n2 = 4;
    cout << "生成 " << n2 << "x" << n2 << " 螺旋矩阵:" << endl;
    vector<vector<int>> result2 = s.generateMatrix(n2);
    for(int i = 0; i < n2; i++)
    {
        for(int j = 0; j < n2; j++)
        {
            cout << result2[i][j] << "\t";
        }
        cout << endl;
    }
    cout << endl;
    
    // 测试案例3：1x1矩阵
    int n3 = 1;
    cout << "生成 " << n3 << "x" << n3 << " 螺旋矩阵:" << endl;
    vector<vector<int>> result3 = s.generateMatrix(n3);
    for(int i = 0; i < n3; i++)
    {
        for(int j = 0; j < n3; j++)
        {
            cout << result3[i][j] << "\t";
        }
        cout << endl;
    }
    cout << endl;
    
    // 测试案例4：2x2矩阵
    int n4 = 2;
    cout << "生成 " << n4 << "x" << n4 << " 螺旋矩阵:" << endl;
    vector<vector<int>> result4 = s.generateMatrix(n4);
    for(int i = 0; i < n4; i++)
    {
        for(int j = 0; j < n4; j++)
        {
            cout << result4[i][j] << "\t";
        }
        cout << endl;
    }
    cout << endl;
    
    return 0;
}
